/*
 * Copyright (C) 2010 Fred Barrie
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.aergia.vinny.util.ValidateUtil;

/**
 * @author Fred
 * 
 */
public class EditorDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4118493049266798376L;
	private JButton cancelButton;
	private final EditorPanel editorPanel;
	private JButton saveButton;
	private boolean saved;

	/**
	 * @param contentPanel
	 */
	public EditorDialog(final EditorPanel editorPanel) {
		ValidateUtil.assertNotNull(editorPanel, "Content panel cannot be NULL");
		this.saved = false;
		this.editorPanel = editorPanel;
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		this.initializeComponents();
	}

	public boolean isSaved() {
		return this.saved;
	}

	protected void cancelAction() {
		this.saved = false;
		this.editorPanel.cancelAction();
		this.dispose();
	}

	protected void saveAction() {
		this.saved = true;
		if (this.editorPanel.saveAction()) {
			this.dispose();
		}
	}

	private JButton getCancelButton() {
		if (this.cancelButton == null) {
			this.cancelButton = new JButton("Cancel");
			this.cancelButton.setMnemonic('C');
			this.cancelButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					EditorDialog.this.cancelAction();
				}
			});
		}
		return this.cancelButton;
	}

	private JButton getSaveButton() {
		if (this.saveButton == null) {
			this.saveButton = new JButton("Save");
			this.saveButton.setMnemonic('S');
			this.saveButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					EditorDialog.this.saveAction();
				}
			});
		}
		return this.saveButton;
	}

	private void initializeComponents() {
		this.setLocationRelativeTo(this.editorPanel);
		this.setLayout(new BorderLayout(5, 5));
		this.setTitle(this.editorPanel.getTitle());

		this.saveButton = this.getSaveButton();
		this.cancelButton = this.getCancelButton();

		final JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 5, 5));
		buttonPanel.add(new JLabel());
		buttonPanel.add(this.saveButton);
		buttonPanel.add(this.cancelButton);

		this.add(this.editorPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);

		this.pack();
		this.setVisible(true);
	}

	public interface Editor {

		/**
		 * Cancel action performed.
		 */
		void cancelAction();

		/**
		 * @return the title for the editor dialog
		 */
		String getTitle();

		/**
		 * Save action performed.
		 */
		boolean saveAction();
	}

	public static abstract class EditorPanel extends JPanel implements Editor {

		/**
		 * Serial Version UID needed for serialization.
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void cancelAction() {
			// empty
		}

	}

}
